package com.example.weather_forecast;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import java.util.Calendar;

public class NotificationScheduler {

    private static final int DAILY_FORECAST_REQUEST_CODE = 1001;

    /**
     * 安排每日天气预报通知
     *
     * @param context     应用上下文
     * @param hourOfDay   通知小时（24小时制）
     * @param minute      通知分钟
     * @param enableSound 是否启用声音
     * @param enableVibration 是否启用振动
     */
    public static void scheduleDailyForecast(Context context, int hourOfDay, int minute,
                                             boolean enableSound, boolean enableVibration) {
        // 首先取消现有的通知计划
        cancelDailyForecastNotification(context);

        // 创建意图
        Intent intent = new Intent(context, NotificationReceiver.class);
        intent.setAction(NotificationReceiver.ACTION_DAILY_FORECAST);
        intent.putExtra(NotificationReceiver.EXTRA_SOUND, enableSound);
        intent.putExtra(NotificationReceiver.EXTRA_VIBRATION, enableVibration);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                context,
                DAILY_FORECAST_REQUEST_CODE,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        // 设置闹钟时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);

        // 如果设置的时间已经过去，则设置为明天的这个时间
        if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }

        // 获取闹钟管理器
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            // 设置每天重复闹钟
            alarmManager.setRepeating(
                    AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY,
                    pendingIntent
            );
        }
    }

    /**
     * 取消每日天气预报通知
     *
     * @param context 应用上下文
     */
    public static void cancelDailyForecastNotification(Context context) {
        Intent intent = new Intent(context, NotificationReceiver.class);
        intent.setAction(NotificationReceiver.ACTION_DAILY_FORECAST);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                context,
                DAILY_FORECAST_REQUEST_CODE,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            alarmManager.cancel(pendingIntent);
        }
        pendingIntent.cancel();
    }

    /**
     * 取消所有已计划的通知
     *
     * @param context 应用上下文
     */
    public static void cancelAllScheduledNotifications(Context context) {
        cancelDailyForecastNotification(context);
        // 如果有其他类型的通知，也在此处取消
    }
}
